package com.soap.desensitize.strategy.irreversible;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import com.soap.desensitize.exception.strategy.EncodeException;

import java.security.NoSuchAlgorithmException;

/**
 * @desc 邮箱加密类
 * @author  buffplum
 */
public class EmailStrategy extends AbstractIRreversibleEncry<String> {
    private static EmailStrategy instance = null;
    private EmailStrategy(){}

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized EmailStrategy getInstance(){
        if(instance == null){
            instance = new EmailStrategy();
        }
        return instance;
    }

    /**
     * @desc 进行邮箱加密（不可逆的）
     * @param data （要被加密的邮箱）
     * @return
     * @throws NoSuchAlgorithmException
     * @throws EncodeException
     */
    @Override
    public String encode(String data) throws EncodeException {
        return emailEncode(data);
    }

    private String emailEncode(String data) throws EncodeException {
        if (data.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){//正则表达式验证邮箱
            String[] strs = data.split("@");

            int dot = strs[1].indexOf(".");
            return strs[0].substring(0,strs[0].length()-4)+"****@"+strs[1];

        }else{
                throw new EncodeException("Mail verification exception");
        }
    }
}
